home *** CD-ROM | disk | FTP | other *** search
- // MemSrch.lib - Provide MemSearch routine to use assembly
- // ver.1 to greatly speed-up search for sub-text
- // within a bigger buffer
-
-
- MemSearchForMem(pBuffer,pBufferLen,pSubBuffer,pSubBufferLen,pCaseSensitive)
- {
- // public _thing
- //
- //ToUpperToLower PROC NEAR
- //Upper_ah:
- // cmp ah, 'a'
- // jb Lower_al
- // cmp ah, 'z'
- // ja Lower_al
- // sub ah, 'a' - 'A'
- //Lower_al:
- // cmp al, 'A'
- // jb UpLowDone
- // cmp al, 'Z'
- // ja UpLowDone
- // add ah, 'a' - 'A'
- //UpLowDone:
- // ret
- //ToUpperToLower ENDP
- //
- //
- //_thing PROC FAR
- //
- // ; assume ax is segment of big buffer
- // ; assume bx is segment of little buffer
- // ; assume cx is length to search first character in big buffer
- // ; assume dx is length of sub-buffer
- //
- // ; set di to location of big buffer so ax:di is big buffer
- // mov di, 0
- // ; set si to location of sub-buffer so bx:si is sub-buffer
- // mov si, 0
- //
- // ; get ds:di to big buffer, and es:si to sub-buffer
- // mov ds, ax
- // mov es, bx
- //
- // ; set al and ah to the byte may starts the sub-buffer, assume
- // ; case insensitive, but CEnvi may overwrite that
- // mov al, es:[si]
- // mov ah, al
- // call ToUpperToLower
- //
- //IsThisTheFirstByte:
- // cmp al, [di]
- // je FirstByteFound
- // cmp ah, [di]
- // jne NotFoundThisByte
- //
- //FirstByteFound:
- // ; found first byte, and so check all the other bytes for a match
- // push di
- // push ax
- // xor bx, bx
- //KeepLookingInSubBuffer:
- // inc bx
- // inc di
- // cmp bx, dx
- // je FoundIt
- // mov al, es:[si+bx]
- // mov ah, al
- // call ToUpperToLower
- // cmp al, [di]
- // je KeepLookingInSubBuffer
- // cmp ah, [di]
- // je KeepLookingInSubBuffer
- //
- //NotThisSubBuffer:
- // pop ax
- // pop di
- //
- //NotFoundThisByte:
- // inc di
- // loop IsThisTheFirstByte
- //
- // ; if here then buffer was never found; return NULL
- // xor ax, ax
- // xor dx, dx
- // jmp ByeBye
- //
- //FoundIt:
- // ; Yea! it was found; return address where it was found
- // pop ax
- // pop ax ; ax = di becaue had pushed di
- // mov dx, ds
- //
- //ByeBye:
- // ret
- //
- //_thing ENDP
- lAsmCode =
- "\x80\xFC\x61\x72\x08\x80\xFC\x7A\x77\x03\x80\xEC\x20\x3C\x41\x72"
- "\x07\x3C\x5A\x77\x03\x80\xC4\x20\xC3\xBF\x00\x00\xBE\x00\x00\x8E"
- "\xD8\x8E\xC3\x26\x8A\x04\x88\xC4\xE8\xD5\xFF\x3A\x05\x74\x04\x3A"
- "\x25\x75\x1C\x57\x50\x31\xDB\x43\x47\x39\xD3\x74\x1B\x26\x8A\x00"
- "\x88\xC4\xE8\xBB\xFF\x3A\x05\x74\xEE\x3A\x25\x74\xEA\x58\x5F\x47"
- "\xE2\xD9\x31\xC0\x31\xD2\xEB\x04\x58\x58\x8C\xDA\xCB";
- // only search first byte up to first one that can fit entire SubBuffer
- lSearchLen = pBufferLen - pSubBufferLen + 1;
- if ( lSearchLen < 0 )
- return NULL;
- // make local copy of assembly so we can change it
- memcpy(lAsm,lAsmCode,1+GetArraySpan(lAsmCode));
- // if case-sensitive then do have case-sensitive sub-routine do nothing
- if ( pCaseSensitive )
- lAsm[0] = '\xC3';
- // set di in code to offst of Buffer, and si to offset of SubBuffer
- BLObPut(lAsm,0x1A,offset(pBuffer),UWORD16);
- BLObPut(lAsm,0x1D,offset(pSubBuffer),UWORD16);
- // call the routine with ax, bx, cx, and dx as assebmly likes it
- lFound = asm(lAsm+0x19,segment(pBuffer),segment(pSubBuffer),
- lSearchLen,pSubBufferLen);
- if ( !lFound )
- return NULL;
- // return variable relative to pBuffer
- return ( pBuffer + (lFound-pointer(pBuffer)) );
- }
-